//
//  TravelsDataBase.m
//  哎游
//
//  Created by 康守帅 on 15-1-13.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "TravelsDataBase.h"
#import "DB.h"

@implementation TravelsDataBase

+ (void)insertIntoUserTravels:(TravelsListModel *)travels user:(NSString *)user
{
    sqlite3 *dbPoint = [DB openDB];
    // 数据库语句
    NSString *sqlStr = [NSString stringWithFormat:@"insert into userTravels(travels_id, travels_backImage, travels_title, travels_date, travels_dateNum, travels_visits, travels_address, travels_userImage, travels_userName, user) values ('%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@')", travels.ID, travels.cover_image_default, travels.name, travels.first_day, travels.day_count, travels.view_count, travels.popular_place_str, [travels.user objectForKey:@"avatar_s"], [travels.user objectForKey:@"name"], user];
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"插入成功");
    }else {
        NSLog(@"插入失败");
    }
}

+ (void)deleteFromUserTravels:(NSString *)traveID user:(NSString *)user
{
    sqlite3 *dbPoint = [DB openDB];
    NSString *sqlStr = [NSString stringWithFormat:@"delete from userTravels where travels_id = %@ and user = '%@'", traveID, user];
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    }else {
        NSLog(@"删除失败");
    }
}

+ (NSArray *)selectUserTravelsWithUser:(NSString *)user
{
    sqlite3 *dbPoint = [DB openDB];
    sqlite3_stmt *stmt = nil;
    NSString *sqlStr = [NSString stringWithFormat:@"select travels_id, travels_backImage, travels_title, travels_date, travels_dateNum, travels_visits, travels_address, travels_userImage, travels_userName from userTravels where user = '%@'", user];
    int result = sqlite3_prepare_v2(dbPoint, [sqlStr UTF8String], -1, &stmt, nil);
    NSMutableArray *array = [NSMutableArray array];
    if (result == SQLITE_OK) {
        // 获得每一条查找到的数据的内容
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *travels_id = sqlite3_column_text(stmt, 0);
            const unsigned char *travels_backImage = sqlite3_column_text(stmt, 1);
            const unsigned char *travels_title = sqlite3_column_text(stmt, 2);
            const unsigned char *travels_date = sqlite3_column_text(stmt, 3);
            const unsigned char *travels_dateNum = sqlite3_column_text(stmt, 4);
            const unsigned char *travels_visits = sqlite3_column_text(stmt, 5);
            const unsigned char *travels_address = sqlite3_column_text(stmt, 6);
            const unsigned char *travels_userImage = sqlite3_column_text(stmt, 7);
            const unsigned char *travels_userName = sqlite3_column_text(stmt, 8);
            
            NSString *travelsID = [NSString stringWithUTF8String:(const char *)travels_id];
            NSString *backImage = [NSString stringWithUTF8String:(const char *)travels_backImage];
            NSString *title = [NSString stringWithUTF8String:(const char *)travels_title];
            NSString *date = [NSString stringWithUTF8String:(const char *)travels_date];
            NSString *dateNum = [NSString stringWithUTF8String:(const char *)travels_dateNum];
            NSString *visits = [NSString stringWithUTF8String:(const char *)travels_visits];
            NSString *address = [NSString stringWithUTF8String:(const char *)travels_address];
            NSString *userImage = [NSString stringWithUTF8String:(const char *)travels_userImage];
            NSString *userName = [NSString stringWithUTF8String:(const char *)travels_userName];
            
            TravelsListModel *travels = [TravelsListModel travelsListWithId:[NSNumber numberWithLongLong:[travelsID longLongValue] ] backImage:backImage title:title date:[NSNumber numberWithInteger:[date integerValue]] dateNum:[NSNumber numberWithInteger:[dateNum integerValue]] visits:[NSNumber numberWithInteger:[visits integerValue]] address:address userImage:userImage userName:userName];
            
            [array addObject:travels];
        }
    }
    
    sqlite3_finalize(stmt);
    
    return array;
}

+ (BOOL)selectForTravelsId:(NSString *)travelsId user:(NSString *)user
{
    BOOL exist = NO;
    sqlite3 *dbPoint = [DB openDB];
    sqlite3_stmt *stmt = nil;
    NSString *sqlStr = [NSString stringWithFormat:@"select * from userTravels where travels_id = '%@' and user = '%@'", travelsId, user];
    int result = sqlite3_prepare_v2(dbPoint, [sqlStr UTF8String], -1, &stmt, nil);
    if (result == SQLITE_OK) {
        if (sqlite3_step(stmt) == SQLITE_ROW) {
            exist = YES;
        }
    }
    sqlite3_finalize(stmt);
    return exist;
}








@end
